namespace _6 {

var convert = function(s: string, numRows: number): string {
    var answer = new Array<string>(numRows)
    for(var i = 0; i < numRows; i++) answer[i] = ""
    
    function zig(tail: string[]) {
        for(var r = 0; r < numRows; r++) {
            if (tail.length === 0) break
            answer[r] += tail.shift()
        }
    }

    function zag(tail: string[]) {
        for(var r = numRows - 2; r > 0; r--) {
            if (tail.length === 0) break
            answer[r] += tail.shift()
        }
    }

    var tail = s.split('')
    while (tail.length > 0) {
        zig(tail)
        zag(tail)
    }
    return answer.join('');
};

console.assert( convert( 'PAYPALISHIRING', 3) === 'PAHNAPLSIIGYIR')

}